﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BaseTool
{
    /// <summary>
    /// 圆角窗体
    /// </summary>
    public partial class InnerForm : Form
    {
        /// <summary> 值为True时，窗体为固定大小，不允许缩放; 值为False时，窗体允许缩放 </summary>
        protected bool IsFixed = false;
        /// <summary> 截图是否显示弹窗 </summary>
        protected bool IsShowScreenMessageBox = true;
        /// <summary> 是否测试状态 </summary>
        protected bool IsTest = false;

        /// <summary> 窗体类型 </summary>
        [BrowsableAttribute(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Bindable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("自定义属性")]
        [Description("窗体类型")]
        public InnerFormType FormType { set; get; } = InnerFormType.完整窗体;

        /// <summary> 设置窗体标题 </summary>
        [BrowsableAttribute(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [Bindable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Category("自定义属性")]
        [Description("文本显示")]
        public override string Text
        {
            set
            {
                if (!string.IsNullOrEmpty(value))
                    this.LabelTitle.Text = value;
                else
                    this.LabelTitle.Text = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name;
                base.Text = value;
            }
            get
            {
                return base.Text;
            }
        }

        /// <summary> 圆角半径 </summary>
        public static int FilletRadius = 15;
        /// <summary> 窗口皮肤 </summary>
        public static InnerFormSkin FormSkin = InnerFormSkin.晴空白;
        /// <summary> [事件]参数变动刷新窗体 </summary>
        public static event Action<bool> EventRefreshForm;
        /// <summary> 是否已初始化 </summary>
        public static bool IsInit = false;
        /// <summary> 下拉弹框 </summary>
        private ContextMenuStrip CMS_Popup = new ContextMenuStrip();

        #region 构造、关闭
        /// <summary> 构造 </summary>
        public InnerForm()
        {
            InitializeComponent();
        }
        //显示前加载
        private void InnerForm_Load(object sender, EventArgs e)
        {
            EventRefreshForm += RefreshForm;
        }
        //显示后加载
        private void InnerForm_Shown(object sender, EventArgs e)
        {
            //初始化控件
            InitForm();
            //刷新控件背景
            RefreshForm(false);
            //刷新右上角图标位置
            panelTitle_SizeChanged(null, null);
        }
        //正在关闭窗口（可中断）
        private void InnerForm_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        //已关闭窗口
        private void InnerForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            EventRefreshForm -= RefreshForm;
        }
        //窗口重绘
        private void InnerForm_Resize(object sender, EventArgs e)
        {
            lock (LockFormScale)
            {
                RefreshForm(false);
            }
        }
        //窗体激活
        private void InnerForm_Activated(object sender, EventArgs e)
        {
            ScreenControl.SetFormScreenSize(this.Location.X, this.Location.Y, this.Size.Width, this.Size.Height);
        }

        #region 窗体圆角
        //设置窗口区域
        private void RefreshFormRegion()
        {
            GraphicsPath FormPath;
            FormPath = new GraphicsPath();
            Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
            FormPath = GetRoundedRectPath(rect, FilletRadius);
            this.Region = new Region(FormPath);
        }
        //设置窗口圆角
        private GraphicsPath GetRoundedRectPath(Rectangle rect, int diameter)
        {
            Rectangle arcRect = new Rectangle(rect.Location, new Size(diameter, diameter));
            GraphicsPath path = new GraphicsPath();
            // 左上角  
            //arcRect.X = 0;
            //arcRect.Y = 0;
            path.AddArc(arcRect, 180, 90);

            // 右上角  
            arcRect.X = rect.Right - diameter;
            //arcRect.Y = 0;
            path.AddArc(arcRect, 270, 90);

            // 右下角  
            //arcRect.X = rect.Right - diameter;
            arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 0, 90);

            // 左下角  
            arcRect.X = rect.Left;
            //arcRect.Y = rect.Bottom - diameter;
            path.AddArc(arcRect, 90, 90);

            path.CloseFigure();//闭合曲线  
            return path;
        }
        #endregion

        #region 窗体移动
        /// <summary> 窗体移动点位 </summary>
        private Point FormMoveOrigionPoint = new Point(0, 0);
        /// <summary> 窗体是否正在移动 </summary>
        private bool IsFormMove = false;
        /// <summary> 事件，主窗口移动，鼠标移动 </summary>
        protected void panelTitle_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsFormMove)
            {
                Point EventLocation = e.Location;
                this.Location = new Point(this.Location.X + EventLocation.X - FormMoveOrigionPoint.X, this.Location.Y - FormMoveOrigionPoint.Y + EventLocation.Y);
            }
        }
        /// <summary> 事件，主窗口移动，鼠标按下 </summary>
        protected void panelTitle_MouseDown(object sender, MouseEventArgs e)
        {
            FormMoveOrigionPoint.X = e.Location.X;
            FormMoveOrigionPoint.Y = e.Location.Y;
            IsFormMove = true;
        }
        /// <summary> 事件，主窗口移动，鼠标释放 </summary>
        protected void panelTitle_MouseUp(object sender, MouseEventArgs e)
        {
            IsFormMove = false;
        }
        /// <summary> 事件，主窗口移动，鼠标进入范围 </summary>
        private void panelTitle_MouseEnter(object sender, EventArgs e)
        {
            Cursor = Cursors.SizeAll;
        }
        /// <summary> 事件，主窗口移动，鼠标退出范围 </summary>
        private void panelTitle_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }
        #endregion

        #region 窗口缩放
        /// <summary> 窗体是否在缩放 </summary>
        private bool IsFormScale = false;
        /// <summary> 窗体边缘状态 </summary>
        private InnerFormMargin FormScaleMargin = InnerFormMargin.正中;
        /// <summary> 窗体缩放原始点位 </summary>
        private Point FormScaleOrigionPoint = new Point(0, 0);
        /// <summary> 窗口缩放锁 </summary>
        private readonly object LockFormScale = new object();

        //事件，鼠标按下
        private void InnerForm_MouseDown(object sender, MouseEventArgs e)
        {
            IsFormScale = true;
            FormScaleOrigionPoint = new Point(e.X, e.Y);
        }
        //事件，鼠标释放
        private void InnerForm_MouseUp(object sender, MouseEventArgs e)
        {
            IsFormScale = false;
        }
        //事件，鼠标移动
        private void InnerForm_MouseMove(object sender, MouseEventArgs e)
        {
            lock (LockFormScale)
            {
                int X = e.X;
                int Y = e.Y;

                //(固定窗体)窗体移动
                if (IsFixed)
                {
                    //窗体移动
                    if (IsFormScale)
                        this.Location = new Point(this.Location.X + X - FormScaleOrigionPoint.X, this.Location.Y + Y - FormScaleOrigionPoint.Y);
                    //鼠标显示十字箭头
                    else
                        Cursor = Cursors.SizeAll;
                    return;
                }

                //窗体缩放
                if (IsFormScale)
                {
                    switch (FormScaleMargin)
                    {
                        case InnerFormMargin.左上:
                            {
                                int sw = FormScaleOrigionPoint.X - X;
                                int sh = FormScaleOrigionPoint.Y - Y;
                                this.Size = new Size(Size.Width + sw, Size.Height + sh);
                                this.Location = new Point(this.Location.X - sw, this.Location.Y - sh);
                                break;
                            }
                        case InnerFormMargin.右上:
                            {
                                //int sw = FormScaleOrigionPoint.X - X;
                                int sh = FormScaleOrigionPoint.Y - Y;
                                this.Size = new Size(X, Size.Height + sh);
                                this.Location = new Point(this.Location.X, this.Location.Y - sh);
                                break;
                            }
                        case InnerFormMargin.右下:
                            {
                                this.Size = new Size(X, Y);
                                break;
                            }
                        case InnerFormMargin.左下:
                            {
                                int sw = FormScaleOrigionPoint.X - X;
                                //int sh = FormScaleOrigionPoint.Y - Y;
                                this.Size = new Size(Size.Width + sw, Y);
                                this.Location = new Point(this.Location.X - sw, this.Location.Y);
                                break;
                            }
                        default:
                            {
                                if (IsTest)
                                    Log.SaveLog($"【error】 {FormScaleMargin} ({FormScaleOrigionPoint.X}, {FormScaleOrigionPoint.Y}) -> ({X} {Y})", LogType.Run, false);
                                return;
                            }
                    }
                    if (IsTest)
                        Log.SaveLog($"{FormScaleMargin} ({FormScaleOrigionPoint.X}, {FormScaleOrigionPoint.Y}) -> ({X} {Y})", LogType.Run, false);
                }
                //修改边缘状态和鼠标显示
                else
                {
                    //第一行
                    if (Y <= FilletRadius)
                    {
                        if (X <= FilletRadius)
                            FormScaleMargin = InnerFormMargin.左上;
                        else if (X >= this.Width - FilletRadius - 1)
                            FormScaleMargin = InnerFormMargin.右上;
                        else
                            FormScaleMargin = InnerFormMargin.上边;
                    }
                    //第三行
                    else if (Y >= this.Height - FilletRadius - 1)
                    {
                        if (X <= FilletRadius)
                            FormScaleMargin = InnerFormMargin.左下;
                        else if (X >= this.Width - FilletRadius - 1)
                            FormScaleMargin = InnerFormMargin.右下;
                        else
                            FormScaleMargin = InnerFormMargin.下边;
                    }
                    //第二行
                    else
                    {
                        if (X <= FilletRadius)
                            FormScaleMargin = InnerFormMargin.左边;
                        else if (X >= this.Width - FilletRadius - 1)
                            FormScaleMargin = InnerFormMargin.右边;
                        else
                            FormScaleMargin = InnerFormMargin.正中;
                    }

                    //鼠标图案
                    switch (FormScaleMargin)
                    {
                        case InnerFormMargin.上边:
                        case InnerFormMargin.下边:
                            {
                                Cursor = Cursors.SizeNS;
                                break;
                            }
                        case InnerFormMargin.左边:
                        case InnerFormMargin.右边:
                            {
                                Cursor = Cursors.SizeWE;
                                break;
                            }
                        case InnerFormMargin.左上:
                        case InnerFormMargin.右下:
                            {
                                Cursor = Cursors.SizeNWSE;
                                break;
                            }
                        case InnerFormMargin.右上:
                        case InnerFormMargin.左下:
                            {
                                Cursor = Cursors.SizeNESW;
                                break;
                            }
                        case InnerFormMargin.正中:
                            {
                                Cursor = Cursors.Default;
                                break;
                            }
                    }

                    if (IsTest)
                        Log.SaveLog($"{FormScaleMargin} ({X} {Y})", LogType.Run, false);
                }
            }
        }
        //事件，鼠标进入范围
        private void InnerForm_MouseEnter(object sender, EventArgs e)
        {

        }
        //事件，鼠标离开范围
        private void InnerForm_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Default;
        }

        #endregion

        #endregion

        #region 初始化、刷新
        /// <summary> 初始化窗体 </summary>
        private void InitForm()
        {
            if (!IsInit)
            {
                //读取本地配置(默认读取)
                //Properties.Settings.Default.Reload();
                FilletRadius = Properties.Settings.Default.FilletRadius;
                Enum.TryParse(Properties.Settings.Default.Skin, out FormSkin);

                //截图控件传入全局参数
                Rectangle rectangle = System.Windows.Forms.Screen.GetBounds(this);
                ScreenControl.SetFullScreenSize(rectangle.Width, rectangle.Height);

                IsInit = true;
            }


            if (FormType == InnerFormType.简单窗体)
            {
                pb_MaxSize.Visible = false;
                pb_Min.Visible = false;
                pb_Pin.Visible = false;
                pb_Select.Visible = false;
            }
            else
            {
                //圆角设置
                ToolStripMenuItem MenuItem1 = new ToolStripMenuItem();
                MenuItem1.Name = "MenuItem1";
                MenuItem1.Text = "圆角设置";
                MenuItem1.DropDownOpening += new EventHandler(MenuItem1_DropDownOpening);
                MenuItem1.DropDownClosed += new EventHandler(MenuItem1_DropDownClosed);
                ToolStripTextBox TextBox1 = new ToolStripTextBox();
                TextBox1.Text = FilletRadius.ToString();
                TextBox1.TextChanged += new EventHandler(TextBox1_TextChanged);
                MenuItem1.DropDownItems.Add(TextBox1);

                //皮肤设置
                ToolStripMenuItem MenuItem2 = new ToolStripMenuItem();
                MenuItem2.Name = "MenuItem2";
                MenuItem2.Text = "皮肤设置";
                MenuItem2.DropDownOpening += new EventHandler(MenuItem2_DropDownOpening);
                //MenuItem2.Visible = false;
                string[] SkinNames = Enum.GetNames(typeof(InnerFormSkin));
                foreach (string skinname in SkinNames)
                {
                    ToolStripMenuItem MenuItemSkin = new ToolStripMenuItem();
                    MenuItemSkin.Name = skinname;
                    MenuItemSkin.Text = skinname;
                    MenuItemSkin.Checked = FormSkin.ToString() == skinname;
                    MenuItemSkin.Click += new EventHandler(MenuItemSkin_Click);
                    MenuItem2.DropDownItems.Add(MenuItemSkin);
                }

                //分割线1
                ToolStripSeparator Separator1 = new ToolStripSeparator();

                //截图（软件窗口）
                ToolStripMenuItem MenuItem3 = new ToolStripMenuItem();
                MenuItem3.Name = "MenuItem3";
                MenuItem3.Text = "截图（软件窗口）";
                MenuItem3.Click += new EventHandler(MenuItem3_Click);

                //截图（全屏）
                ToolStripMenuItem MenuItem4 = new ToolStripMenuItem();
                MenuItem4.Name = "MenuItem4";
                MenuItem4.Text = "截图（全屏）";
                MenuItem4.Click += new EventHandler(MenuItem4_Click);

                //截图（自定义）
                ToolStripMenuItem MenuItem5 = new ToolStripMenuItem();
                MenuItem5.Name = "MenuItem5";
                MenuItem5.Text = "截图（自定义）";
                MenuItem5.Click += new EventHandler(MenuItem5_Click);

                //分割线2
                ToolStripSeparator Separator2 = new ToolStripSeparator();

                //打开程序文件夹
                ToolStripMenuItem MenuItem6 = new ToolStripMenuItem();
                MenuItem6.Name = "MenuItem6";
                MenuItem6.Text = "打开程序文件夹";
                MenuItem6.Click += new EventHandler(MenuItem6_Click);

                //打开参数文件夹
                ToolStripMenuItem MenuItem7 = new ToolStripMenuItem();
                MenuItem7.Name = "MenuItem7";
                MenuItem7.Text = "打开参数文件夹";
                MenuItem7.Click += new EventHandler(MenuItem7_Click);

                //打开数据文件夹
                ToolStripMenuItem MenuItem8 = new ToolStripMenuItem();
                MenuItem8.Name = "MenuItem8";
                MenuItem8.Text = "打开数据文件夹";
                MenuItem8.Click += new EventHandler(MenuItem8_Click);

                //分割线3
                ToolStripSeparator Separator3 = new ToolStripSeparator();

                //日志栏显示/隐藏
                ToolStripMenuItem MenuItem9 = new ToolStripMenuItem();
                MenuItem9.Name = "MenuItem9";
                MenuItem9.Text = "日志栏隐藏";
                MenuItem9.Click += new EventHandler(MenuItem9_Click);

                //分割线4
                ToolStripSeparator Separator4 = new ToolStripSeparator();

                //一键打包数据
                ToolStripMenuItem MenuItem10 = new ToolStripMenuItem();
                MenuItem10.Name = "MenuItem10";
                MenuItem10.Text = "一键打包数据";
                string[] packageNames = new string[] { "日志和配方", "全部数据" };
                foreach (string packageName in packageNames)
                {
                    ToolStripMenuItem MenuItemPack = new ToolStripMenuItem();
                    MenuItemPack.Name = packageName;
                    MenuItemPack.Text = packageName;
                    MenuItemPack.Click += new EventHandler(MenuItemPack_Click);
                    MenuItem10.DropDownItems.Add(MenuItemPack);
                }

                if (CMS_Popup.Items != null && CMS_Popup.Items.Count > 0)
                    CMS_Popup.Items.Clear();
                CMS_Popup.Items.AddRange(new ToolStripItem[] {
                    MenuItem1,
                    MenuItem2,
                    Separator1,
                    MenuItem3,
                    MenuItem4,
                    MenuItem5,
                    Separator2,
                    MenuItem6,
                    MenuItem7,
                    MenuItem8,
                    Separator3,
                    MenuItem9,
                    Separator4,
                    MenuItem10,
                });
            }
        }

        /// <summary>
        /// 刷新窗体
        /// </summary>
        /// <param name="isSave"> 是否需要保存本地配置 </param>
        private void RefreshForm(bool isSave)
        {
            //刷新圆角设置
            RefreshFormRegion();

            //背景控件圆角刷新
            RefreshPanelBackground(panelBackground, FilletRadius);
            //标题控件圆角刷新
            RefreshPanelTitle(panelTitle, FilletRadius);

            //刷新皮肤
            RefreshFormSkin();

            //保存本地配置
            if (isSave)
            {
                Properties.Settings.Default.FilletRadius = FilletRadius;
                Properties.Settings.Default.Skin = FormSkin.ToString();
                Properties.Settings.Default.Save();
            }
        }
        /// <summary> 背景控件刷新 </summary>
        private void RefreshPanelBackground(Control sender, int radius)
        {
            int diameter = radius;
            //Rectangle arcRect = new Rectangle(sender.Location, new Size(diameter, diameter));
            Rectangle arcRect = new Rectangle(new Point(0, 0), new Size(diameter, diameter));
            GraphicsPath oPath = new GraphicsPath();

            int _height = sender.Height;
            int _height2 = _height - diameter;
            int _width = sender.Width;
            int _width2 = _width - diameter;

            oPath.AddLine(new Point(0, _height2), new Point(0, 0));
            oPath.AddLine(new Point(0, 0), new Point(_width, 0));
            oPath.AddLine(new Point(_width, 0), new Point(_width, _height2));
            arcRect.X = _width2;
            arcRect.Y = _height2;
            oPath.AddArc(arcRect, 0, 90);
            oPath.AddLine(new Point(_width2, _height), new Point(_width2, _height));
            arcRect.X = 0;
            arcRect.Y = _height2;
            oPath.AddArc(arcRect, 90, 90);
            oPath.CloseFigure();//闭合曲线  

            sender.Region = new Region(oPath);
        }
        /// <summary> 标题控件刷新 </summary>
        private void RefreshPanelTitle(Control sender, int radius)
        {
            int diameter = radius;
            //Rectangle arcRect = new Rectangle(sender.Location, new Size(diameter, diameter));
            Rectangle arcRect = new Rectangle(new Point(0, 0), new Size(diameter, diameter));
            GraphicsPath oPath = new GraphicsPath();

            int _height = sender.Height;
            int _height2 = _height - diameter;
            int _width = sender.Width;
            int _width2 = _width - diameter;

            arcRect.X = 0;
            arcRect.Y = 0;
            oPath.AddArc(arcRect, 180, 90);
            oPath.AddLine(new Point(diameter, 0), new Point(_width2, 0));
            arcRect.X = _width2;
            arcRect.Y = 0;
            oPath.AddArc(arcRect, 270, 90);
            oPath.AddLine(new Point(_width, diameter), new Point(_width, _height));
            oPath.AddLine(new Point(_width, _height), new Point(0, _height));
            oPath.AddLine(new Point(0, _height), new Point(0, diameter));

            oPath.CloseFigure();//闭合曲线  

            sender.Region = new Region(oPath);
        }
        #endregion

        #region 皮肤/颜色
        /// <summary> 颜色 - 标题栏 </summary>
        protected Color ColorTitle;
        /// <summary> 颜色 - 背景 </summary>
        protected Color ColorBackground;
        /// <summary> 颜色 - 图片选中颜色 </summary>
        protected Color ColorPictureBoxSelect;

        /// <summary> 设置颜色 </summary>
        private void RefreshFormSkin()
        {
            switch (FormSkin)
            {
                case InnerFormSkin.晴空白:
                    {
                        ColorTitle = Color.White;
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.DodgerBlue;
                        break;
                    }
                case InnerFormSkin.星空黑:
                    {
                        ColorTitle = Color.FromArgb(80, 80, 80);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(30, 30, 30);
                        break;
                    }
                case InnerFormSkin.极光蓝:
                    {
                        ColorTitle = Color.FromArgb(152, 192, 252);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(44, 126, 248);
                        break;
                    }
                case InnerFormSkin.阳光橙:
                    {
                        ColorTitle = Color.FromArgb(255, 215, 176);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(255, 129, 24);
                        break;
                    }
                case InnerFormSkin.开源绿:
                    {
                        ColorTitle = Color.FromArgb(171, 245, 209);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(54, 179, 126);
                        break;
                    }
                case InnerFormSkin.暮光紫:
                    {
                        ColorTitle = Color.FromArgb(192, 182, 242);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(101, 84, 192);
                        break;
                    }
                case InnerFormSkin.梅子红:
                    {
                        ColorTitle = Color.FromArgb(224, 186, 208);
                        ColorBackground = Color.FromArgb(238, 238, 242);
                        ColorPictureBoxSelect = Color.FromArgb(178, 85, 120);
                        break;
                    }
            }
            this.BackColor = ColorTitle;
            panelTitle.BackColor = ColorTitle;
            panelBackground.BackColor = ColorBackground;
        }
        #endregion

        #region 图片事件
        //图片定位
        private void panelTitle_SizeChanged(object sender, EventArgs e)
        {
            PictureBox[] pbs = new PictureBox[] { pb_Close, pb_MaxSize, pb_Min, pb_Pin, pb_Select };
            int width = panelTitle.Size.Width;
            foreach (var pb in pbs)
            {
                if (pb == null || !pb.Visible)
                    continue;
                pb.Location = new Point(width - pb.Width, 0);
                width -= pb.Width;
            }
        }
        //图片点击
        private void pb_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            switch (pb.Name)
            {
                case "pb_Close":
                    {
                        this.Close();
                        break;
                    }
                case "pb_MaxSize":
                    {
                        if (IsFixed) return;
                        if (this.WindowState == FormWindowState.Normal)
                            this.WindowState = FormWindowState.Maximized;
                        else
                            this.WindowState = FormWindowState.Normal;
                        RefreshForm(false);
                        break;
                    }
                case "pb_Min":
                    {
                        this.WindowState = FormWindowState.Minimized;
                        break;
                    }
                case "pb_Pin":
                    {
                        this.TopMost = !this.TopMost;
                        pb_Pin.BackgroundImage = this.TopMost ? Properties.Resources.图钉1 : Properties.Resources.图钉0;
                        break;
                    }
            }
        }
        //图片进入
        private void pb_MouseEnter(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.BackColor = ColorPictureBoxSelect;
            if (pb.Name == "pb_Select")
                CMS_Popup.Show(pb_Select, 0, pb_Select.Height);
        }
        //图片离开
        private void pb_MouseLeave(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            pb.BackColor = panelTitle.BackColor;
        }

        #endregion

        #region 下拉框事件
        /// <summary>
        /// 外部触发参数变动刷新窗体
        /// </summary>
        public static void StartEventRefreshForm()
        {
            EventRefreshForm?.Invoke(true);
        }

        /// <summary> 是否正在刷新下拉框 </summary>
        private bool IsRefreshCMS = false;
        /// <summary> 圆角设置 - 刷新值 </summary>
        private void MenuItem1_DropDownOpening(object sender, EventArgs e)
        {
            try
            {
                IsRefreshCMS = true;
                //圆角设置
                ((ToolStripTextBox)((ToolStripMenuItem)CMS_Popup.Items[0]).DropDownItems[0]).Text = FilletRadius.ToString();
            }
            catch { }
            finally { IsRefreshCMS = false; }
        }
        /// <summary> 圆角设置 - 设置框关闭 </summary>
        private void MenuItem1_DropDownClosed(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem mi = (ToolStripMenuItem)sender;
                if (((ToolStripTextBox)mi.DropDownItems[0]).Text != FilletRadius.ToString())
                {
                    IsRefreshCMS = true;
                    ((ToolStripTextBox)mi.DropDownItems[0]).Text = FilletRadius.ToString();
                }
            }
            catch { }
            finally { IsRefreshCMS = false; }
        }
        /// <summary> 圆角设置 - 文本框参数变动 </summary>
        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            if (IsRefreshCMS) return;

            ToolStripTextBox tb = (ToolStripTextBox)sender;
            if (string.IsNullOrWhiteSpace(tb.Text))
            {
                tb.BackColor = Color.Red;
                return;
            }
            int value;
            if (!int.TryParse(tb.Text, out value) || value <= 0)
            {
                tb.BackColor = Color.Red;
                return;
            }
            tb.BackColor = SystemColors.Window;
            FilletRadius = value;
            EventRefreshForm?.Invoke(true);
        }

        /// <summary> 皮肤设置 - 选择框打开 </summary>
        private void MenuItem2_DropDownOpening(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem MenuItem2 = (ToolStripMenuItem)sender;
                foreach (ToolStripMenuItem item in MenuItem2.DropDownItems)
                {
                    item.Checked = item.Text == FormSkin.ToString();
                }
            }
            catch { }
        }
        /// <summary> 皮肤设置 - 颜色点击 </summary>
        private void MenuItemSkin_Click(object sender, EventArgs e)
        {
            try
            {
                ToolStripMenuItem MenuItemSkin = (ToolStripMenuItem)sender;
                Enum.TryParse(MenuItemSkin.Text, out FormSkin);
                ToolStripMenuItem MenuItem2 = (ToolStripMenuItem)sender;
                foreach (ToolStripMenuItem item in MenuItem2.DropDownItems)
                {
                    item.Checked = item.Text == FormSkin.ToString();
                }
                EventRefreshForm?.Invoke(true);
            }
            catch { }
        }

        /// <summary> 截图（软件窗口） </summary>
        private void MenuItem3_Click(object sender, EventArgs e)
        {
            string msg = ScreenControl.ScreenForm();
            Log.SaveLog(msg, LogType.Run, Color.Black);
            if (IsShowScreenMessageBox)
                MessageBox.Show(msg);
        }
        /// <summary> 截图（全屏） </summary>
        private void MenuItem4_Click(object sender, EventArgs e)
        {
            string msg = ScreenControl.ScreenFull();
            Log.SaveLog(msg, LogType.Run, Color.Black);
            if (IsShowScreenMessageBox)
                MessageBox.Show(msg);
        }
        /// <summary> 截图（自定义） </summary>
        private void MenuItem5_Click(object sender, EventArgs e)
        {
            ScreenCustomRoiForm scr = new ScreenCustomRoiForm();
            scr.ShowDialog();
            Log.SaveLog(ScreenCustomRoiForm.ErrorMessage, LogType.Run, Color.Black);
            if (IsShowScreenMessageBox)
                MessageBox.Show(ScreenCustomRoiForm.ErrorMessage);
        }
        /// <summary> 打开程序文件夹 </summary>
        private void MenuItem6_Click(object sender, EventArgs e)
        {
            if (!FileTool.OpenFolder(PathTool.BaseDirectory, false))
                MessageBox.Show($"打开程序文件夹失败，请检测路径!\r\n{PathTool.BaseDirectory}");
        }
        /// <summary> 打开参数文件夹 </summary>
        private void MenuItem7_Click(object sender, EventArgs e)
        {
            if (!FileTool.OpenFolder(PathTool.AllTypePath, false))
                MessageBox.Show($"打开参数文件夹失败，请检测路径!\r\n{PathTool.AllTypePath}");
        }
        /// <summary> 打开数据文件夹 </summary>
        private void MenuItem8_Click(object sender, EventArgs e)
        {
            if (!FileTool.OpenFolder(PathTool.DataPath, true))
                MessageBox.Show($"打开数据文件夹失败，请检测路径!\r\n{PathTool.DataPath}");
        }
        /// <summary> 日志控件显示/隐藏 </summary>
        private void MenuItem9_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menuItem = (ToolStripMenuItem)sender;
            menuItem.Text = Machine.LogControlVisableReadOnly ? "日志栏显示" : "日志栏隐藏";
            Machine.LogControlVisableChange?.Invoke();
        }
        /// <summary> 一键打包数据 </summary>
        private void MenuItemPack_Click(object sender, EventArgs e)
        {
            switch (((ToolStripMenuItem)sender).Text)
            {
                case "日志和配方":
                    {
                        FileTool.AutomaticDataPackaging(false);
                        break;
                    }
                case "全部数据":
                    {
                        FileTool.AutomaticDataPackaging(true);
                        break;
                    }
            }
        }
        #endregion

    }

    #region 枚举
    /// <summary> 窗体类型 </summary>
    public enum InnerFormType
    {
        /// <summary> 完整窗体 </summary>
        完整窗体,
        /// <summary> 简单窗体 </summary>
        简单窗体
    }
    /// <summary> 窗体皮肤 </summary>
    public enum InnerFormSkin
    {
        /// <summary> 晴空白 </summary>
        晴空白,
        /// <summary> 极光蓝 </summary>
        极光蓝,
        /// <summary> 阳光橙 </summary>
        阳光橙,
        /// <summary> 开源绿 </summary>
        开源绿,
        /// <summary> 暮光紫 </summary>
        暮光紫,
        /// <summary> 梅子红 </summary>
        梅子红,
        /// <summary> 星空黑 </summary>
        星空黑
    }
    /// <summary> 窗口边缘位置 </summary>
    public enum InnerFormMargin
    {
        /// <summary> 左上 </summary>
        左上,
        /// <summary> 上边 </summary>
        上边,
        /// <summary> 右上 </summary>
        右上,
        /// <summary> 右边 </summary>
        右边,
        /// <summary> 右下 </summary>
        右下,
        /// <summary> 下边 </summary>
        下边,
        /// <summary> 左下 </summary>
        左下,
        /// <summary> 左边 </summary>
        左边,
        /// <summary> 正中 </summary>
        正中
    }
    #endregion
}
